import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.*;
/* if Person went to Jail, he loses his turn right away.
 * on his next turn he can choose to either pay right away to get out 
 * or gamble to roll doubles to get out
 *    */
public class diceUI extends JPanel {
	private BoardUi board;
	private JButton btnRoll;
	private Dice d = new Dice(1,1);
	
	public diceUI(BoardUi board_) {
		
		this.board=board_;
		
		final String FILE_PREFIX = "/images/die";
		final String FILE_SUFFIX = ".jpg";
		setBorder(new LineBorder(new Color(0, 0, 0), 2));
		setBackground(new Color(192, 192, 192));
		setLayout(null);
		
		final JLabel lblDie2 = new JLabel("");
		lblDie2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		lblDie2.setBounds(83, 63, 53, 53);
		lblDie2.setIcon(new ImageIcon(diceUI.class.getResource("/images/die2.jpg")));
		add(lblDie2);
		
		final JLabel lblDie1 = new JLabel("");
		lblDie1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		lblDie1.setBounds(10, 63, 53, 53);
		lblDie1.setIcon(new ImageIcon(diceUI.class.getResource("/images/die1.jpg")));
		add(lblDie1);
		
		final JLabel lblTotal = new JLabel("TOTAL:");
		lblTotal.setBackground(new Color(255, 255, 255));
		lblTotal.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		lblTotal.setOpaque(true);
		lblTotal.setBounds(10, 127, 126, 29);
		lblTotal.setForeground(Color.BLACK);
		lblTotal.setFont(new Font("Tahoma", Font.BOLD, 12));
		add(lblTotal);
		
		btnRoll = new JButton("ROLL");
		btnRoll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(board.getCurrentPlayer().getInJail()!=true && board.getCurrentPlayer().getDoubleRoll()<3){
					board.moveToken(d.roll());
					btnRoll.setVisible(false);
					
					lblDie1.setIcon(new ImageIcon(diceUI.class.getResource(FILE_PREFIX + d.getRandom1()+ FILE_SUFFIX)));
					lblDie2.setIcon(new ImageIcon(diceUI.class.getResource(FILE_PREFIX + d.getRandom2()+ FILE_SUFFIX)));
					lblTotal.setText("TOTAL:	" + d.getResult());
					
					checkDouble();
				}else if(board.getCurrentPlayer().getDoubleRoll()==3 && board.getCurrentPlayer().getHasGetOutOfJailCard()==0 && board.getCurrentPlayer().getInJail()!=true){
					JOptionPane.showMessageDialog(null,"Triple Double rolls ka,Punta ka sa Jail!");
					board.getCurrentPlayer().setDoubleRoll(0);
					board.getCurrentPlayer().setInJail(true);
					board.goingToJail();
					btnRoll.setVisible(false);
					board.getGameButtonUI().enableEndTurnBtn();
				}else if(board.getCurrentPlayer().getDoubleRoll()==3 && board.getCurrentPlayer().getHasGetOutOfJailCard()>0 && board.getCurrentPlayer().getInJail()!=true){
						board.getCurrentPlayer().setDoubleRoll(0);
						JOptionPane.showMessageDialog(null,"You used JAIL FREE CARD ");
						board.getCurrentPlayer().setHasGetOutOfJailCard(board.getCurrentPlayer().getDoubleRoll()-1);
						board.getCurrentPlayer().setInJail(false);
						board.getGameButtonUI().enableEndTurnBtn();	
						board.MoveOutOfJail();
						btnRoll.setVisible(false);
				}else if(board.getCurrentPlayer().getInJail()==true ){
					d.roll();
					rollOutOfJail();
				}
			}
		});
		btnRoll.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnRoll.setForeground(Color.RED);
		btnRoll.setBounds(10, 11, 126, 41);
		btnRoll.setFont(new Font("Verdana", Font.BOLD, 16));
		add(btnRoll);

	}
	
	public void checkDouble() {
		if(d.getRandom1()==d.getRandom2()&& board.getCurrentPlayer().getDoubleRoll()< 3){
			board.getCurrentPlayer().setDoubleRoll(board.getCurrentPlayer().getDoubleRoll()+1);
			JOptionPane.showMessageDialog(null,"Doubles, Pwuede ka pang mag-Roll!");
		}else if(d.getRandom1()!=d.getRandom2()){
			board.getCurrentPlayer().setDoubleRoll(0);
		}
	}
	
	public void rolledDoubles(){
		if(d.getRandom1()== d.getRandom2()){
			board.getGameButtonUI().disableEndTurnBtn();
			btnRoll.setVisible(true);
		}else{
			board.getGameButtonUI().enableEndTurnBtn();
		}
	}
	
	public void rollOutOfJail(){
		if(d.getRandom1()==d.getRandom2()){
			JOptionPane.showMessageDialog(null,"You rolled Doubles, Wala ka na sa Jail!");
			board.MoveOutOfJail();
			board.getCurrentPlayer().setJailRollCount(0);
			btnRoll.setVisible(false);
			board.getCurrentPlayer().setInJail(false);
			
		}else if(d.getRandom1()!=d.getRandom2() && board.getCurrentPlayer().getJailRollCount()< 3 ){
			board.getCurrentPlayer().setJailRollCount(board.getCurrentPlayer().getJailRollCount()+1);
			btnRoll.setVisible(false);
			JOptionPane.showMessageDialog(null,"You didn't roll Doubles, You lose Turn!");
			board.getGameButtonUI().enableEndTurnBtn();
			board.getInJailUI().setVisible(false); //if you choose roll and and double, you can't have options to pay already.
			
			if(board.getCurrentPlayer().getJailRollCount()==3){
				JOptionPane.showMessageDialog(null,"3 tries ka na, you're free");
				board.MoveOutOfJail();
				board.getCurrentPlayer().setJailRollCount(0);
				btnRoll.setVisible(false);
				board.getGameButtonUI().enableEndTurnBtn();	
			}
		}
	}
	public void enableRollBtn(){
		this.btnRoll.setVisible(true);
	}
	
	public void disableRollBtn(){
		this.btnRoll.setVisible(false);
	}
}
